import requests
import json

def send_dingtalk_msg(webhook_url, msg,at_mobiles=None, is_at_all=False):
    headers = {'Content-Type': 'application/json', 'Charset': 'UTF-8'}
    data = {
        "msgtype": "text",
        "text": {
            "content": msg
        }
        ,
        "at": {
            "atMobiles": at_mobiles or [],  # 需要@的手机号列表
            "isAtAll": is_at_all  # 是否@全体成员
        }
    }
    response = requests.post(url=webhook_url, data=json.dumps(data), headers=headers)
    if response.status_code == 200:
        print("Message sent successfully.")
    else:
        print(f"Failed to send message. Status code: {response.status_code}")

if __name__ == '__main__':
    webhook_url = r"https://oapi.dingtalk.com/robot/send?access_token=f47ccdf5b869cb1c6b1b17aa1542350e44d45588171116e219dd9e152fdcaacd"
    message = "Hello from DingTalk robot!"
    token='SEC9b11599b47b9c24b12f619f832b7aa3be15209d4b97f3ccfd56df1953f949036'
    send_dingtalk_msg(webhook_url, message,[18617085662,18681557567])